using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Recruiters_jobreply : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;

        Page searchPage = this.PreviousPage;
        if (!IsPostBack)
        {
            LoadDataForDropDownList();
            ShowLetter(ddlTemplate.SelectedValue);
        }
        if (searchPage != null)
        {
            string emails = GetEmails(searchPage);
            if (emails != "")
            {
                emails = emails.Substring(0, emails.Length - 1);
                txtEmails.Text = emails;
            }
            else
                Response.Redirect("jobresponses.aspx");
        }
        else
            {
                string email = Request.QueryString.Get("email");
                txtEmails.Text  = email;
            }
    }
    string GetEmails(Page searchPage)
    {
        
        GridView gridView = (GridView)
            searchPage.FindControl("GridView1");
        string emails = "";
        foreach (GridViewRow gridViewRow
         in gridView.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkResumeID");

            if (checkBox.Checked)
            {

                try
                {
                    HyperLink hyperLink = (HyperLink)
                    gridViewRow.Cells[3].Controls[0];
                    emails += hyperLink.Text + ",";
                }
                catch (Exception ex)
                {
                    litError.Text = ex.Message;
                }
            }

        }
        return emails;
    }
    void ShowLetter(string letterID)
    {
        try
        {
            SqlServerProvider cls = new SqlServerProvider();
            cls.ConnectionString = connectionString;

            cls.CommandText = "select * from " +
            "tblResponseLetters where RecruiterID="
             + Session["rcuserid"].ToString() +
             " and ResponseID='" + letterID + "'";
            DataRow dataRow = cls.GetDataRow();
            if (dataRow != null)
            {
                txtSubject.Text =
                dataRow["ResponseSubject"].ToString();
                txtBody.Text =
                dataRow["ResponseBody"].ToString();
                txtSignature.Text =
                dataRow["ResponseSignature"].ToString();
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    void LoadDataForDropDownList()
    {
        try
        {
            Common cls = new Common(connectionString);
            DataTable dataTable = cls.GetDataTable(
            "select * from tblResponseLetters" +
            " where RecruiterID=" +
            Session["rcuserid"].ToString());
            DropDownListHelper helper =
            new DropDownListHelper();
            helper.FillDataTableToDropDownList(
            dataTable, ddlTemplate,
            "ResponseSubject",
            "ResponseID", "");
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }

    }



    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            string emails = txtEmails.Text;
            MailProvider mailProvider = new MailProvider();
            if (mailProvider.SendMail(emails,
            txtCc.Text, "", txtSubject.Text,
            "Dear \n" + txtBody + txtSignature.Text))
                Response.Redirect("jobresponses.aspx");
            else
                litError.Text = mailProvider.ErrorMessage;
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    protected void ddlTemplate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowLetter(ddlTemplate.SelectedValue);
    }
}
